package cc.wubo.jpa.service;

import javax.persistence.Cache;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cc.wubo.jpa.entity.Book;
@Service
public class BookService {

	@Autowired
	private EntityManager em;
	@Autowired
	private EntityManagerFactory emf;
	@Transactional
	public void cache(){
		Book book = em.find(Book.class, 1);
		System.out.println(book);
		book = em.find(Book.class, 1);
		System.out.println(book);
	}
	public void list(){
		Query q= em.createQuery("from Book");
		q.setHint("org.hibernate.cacheable", "true");
		q.getResultList();
	}
	public void jpaCache(){
		Cache cache = emf.getCache();
		System.out.println(cache.contains(Book.class,1));
		cache.evict(Book.class, 1);
	}
}
